package com.example.airport.net.datarequest;


import com.example.airport.net.G;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/***
 * 网络请求封装
 */
public class Http {

    int index = 0;
    IBack show;
    IRequest request;
    Retrofit retrofit;
    String url;


    public Http(String base_url,String url, IBack show, int index) {
        this.show = show;
        this.index = index;
        this.url = url;

        //System.out.println(G.BASE_URL);
        //String base_url=G.BASE_URL;
        //if(url.indexOf("http")==0) base_url="";
        //String base_url="http://101.132.122.53:10023/";
        OkHttpClient mClient  = new OkHttpClient.Builder()
                .connectTimeout(1, TimeUnit.MINUTES)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS)
                .callTimeout(2, TimeUnit.MINUTES)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(base_url) // 设置 网络请求 Url
                .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
                .client(mClient)
                .build();
        request = retrofit.create(IRequest.class);

    }

    public void httpGet() {

        Call<ResponseBody> call = request.get(url);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {

                    if (response.body() != null) {
                        show.success(index, response.body());

                    } else {

                    }
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println("连接失败");
                System.out.println(throwable.toString());
                show.failure(0,"网络连接失败");
            }
        });


    }


    public void httpPost(long timestamp,RequestBody body) {
        Call<ResponseBody> call = request.post(timestamp,url,body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.body() != null) {
                        show.success(index, response.body());
                    } else {
                    }
                } catch (Exception e) {

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                // System.out.println("连接失败");
                show.failure(0,"网络连接失败");
            }
        });
    }



}
